"""
Copyright 2009 Texas A&M University
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0
  
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License. 
"""

from os import path

from logging import getLogger

from nevow import tags
from nevow.athena import LivePage, AutoJSPackage
from nevow.loaders import stan
from nevow.static import File

from meercat.server.nevow.elements import *
from meercat.server.nevow.rest import RestGenerator



log = getLogger('meercat.server.nevow.page')

class Root(LivePage):
    docFactory = stan(tags.html[
        tags.head(render=tags.directive('liveglue'))[
            tags.title()['Meercat Manager'],
            tags.link(rel='stylesheet',type='text/css',
                      href='/css/reset-fonts-grids.css'),
            tags.link(rel='stylesheet',type='text/css', href='/css/base.css'),
            tags.script(type='text/javascript', src='/js/jquery.js'),
            tags.script(type='text/javascript', src='/js/tools.overlay.js'),
            tags.script(type='text/javascript', src='/js/main.js')],
        
        tags.body[
            tags.div(id='custom-doc',class_='yui-t5')[
                tags.div(id='hd')["Meercat Manager"],
                tags.div(id='bd')[
                    tags.div(id='yui-main')[
                        tags.div(id='main-content',class_='yui-b')[
                            tags.div(class_='yui-gb')[
                                tags.div(class_='yui-u first')[
                                    tags.h1()['Storage'],
                                    tags.div(render=tags.directive('storageList'))
                                ],
                                tags.div(class_='yui-u')[
                                    tags.h1()['Harvesters'],
                                    tags.div(render=tags.directive('harvesterList'))
                                ],
                                tags.div(class_='yui-u')[
                                    tags.h1()['Jobs'],
                                    tags.div(render=tags.directive('jobList'))
                                ],
                            ],
                            tags.div(class_='yui-gb')[
                                tags.div(class_='yui-u first')[
                                    tags.h1()['Mappers'],
                                    tags.div(render=tags.directive('mapperList'))
                                ],
                                tags.div(class_='yui-u')[
                                    tags.h1()['Reducers'],
                                    tags.div(render=tags.directive('reducerList'))
                                ],
                                tags.div(class_='yui-u')[
                                    tags.h1()['Processes'],
                                    tags.div(render=tags.directive('processList'))
                                ],
                            ],
                        ]
                    ],
                    tags.div(class_='yui-b')[
                        tags.h1()['Actions'],
                    ],
                ]
            ]
        ]
    ])
    
    addSlash = True
    
    children = {
        'css': File(path.join(path.dirname(path.abspath(__file__)),'css')),
        'js': File(path.join(path.dirname(path.abspath(__file__)),'js')),
        'rest': RestGenerator(),
        'solr': RestGenerator('solr')}
    
    def __init__(self, jobService, username=None, *args, **kwargs):
        super(Root, self).__init__(*args,**kwargs)
        
        self._jobService = jobService
        self._username = username
        
        self.jsModules.mapping.update(AutoJSPackage(path.join(
            path.dirname(path.abspath(__file__)),'widgets')).mapping)
    
    def getJobService(self):
        return self._jobService
    
    def getApplication(self):
        return self.getJobService().parent
    
    def render_harvesterList(self, ctx, data):
        f = harvester.HarvesterList()
        f.setFragmentParent(self)
        
        return ctx.tag[f]
    
    def render_storageList(self, ctx, data):
        f = StorageList()
        f.setFragmentParent(self)
        
        return ctx.tag[f]
    
    def render_jobList(self, ctx, data):
        f = JobList()
        f.setFragmentParent(self)
        
        return ctx.tag[f]
    
    def render_mapperList(self, ctx, data):
        f = MapperList()
        f.setFragmentParent(self)
        
        return ctx.tag[f]
    
    def render_reducerList(self, ctx, data):
        f = ReducerList()
        f.setFragmentParent(self)
        
        return ctx.tag[f]
    
    def render_processList(self, ctx, data):
        f = ActiveProcessList()
        f.setFragmentParent(self)
        
        return ctx.tag[f]
    
    def child_(self, ctx):
        return Root(self._jobService, self._username)
    
    def childFactory ( self, ctx, name ):
        return RestGenerator(name)
